<?php

namespace plugin\admin\app\service\v1;

use plugin\admin\app\constant\v1\CommonConstant;

/**
 * 支付服务类
 **/
class PaymentService
{

    /**
     * 支付入口
     *
     * @param string $pay_type 支付方式
     * @param string $app_type 支付类型
     * @param array $params 所需参数
     */
    public static function pay($pay_type, $app_type, $params)
    {
        $config = OptionService::getConfig('pay');
        switch ($pay_type) {
            case CommonConstant::PAYTYPE_ALIPAY:
                $result = self::alipayPc($config, $params);
                return $result;
                break;
            case CommonConstant::PAYTYPE_WECHAT:
                $result = self::wechatpayNative($config, $params);
                return $result;
                break;
        }
    }

    /**
     * 支付宝支付 网站支付
     *
     * @param array $config
     * @param array $params 所需参数
     **/
    public static function alipayPc($config, $params)
    {
        $notifyUrl = $config['alipay_notify_url'];
        $aliPay = new \plugin\admin\app\common\alipay\Pc();
        $aliPay->setAppid($config['alipay_app_id']);
        $aliPay->setRsaPrivateKey($config['alipay_app_secret_cert']);
        $aliPay->setNotifyUrl($notifyUrl);
        $aliPay->setOutTradeNo($params['orderNo']);
        $aliPay->setTotalFee($params['amount']);
        $aliPay->setSubject($params['subject']);
        $sHtml = $aliPay->goPay();
        return $sHtml;
    }

    /**
     * 微信支付 扫码支付
     *
     * @param array $config
     * @param array $params 所需参数
     */
    public static function wechatpayNative($config, $params)
    {
        $wxPay = new \plugin\admin\app\common\wechatpay\Native($config['wechat_mch_id'], $config['wechat_app_id'], $config['wechat_mch_secret_key_v2']);
        $notifyUrl = $config['wechat_notify_url'];
        $payTime = time();
        $array = $wxPay->createJsBizPackage($params['amount'], $params['orderNo'], $params['subject'], $notifyUrl, $payTime);
        $qr = $array['code_url'];
        return $qr;
    }

}
